home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / appl / napsaterm / clip.c < prev    next >
C/C++ Source or Header  |  1994-05-14  |  4KB  |  147 lines

  1. RCS_ID_C "$Id: clip.c,v 2.0 1993/11/15 03:35:51 ppessi Exp $";
  2. /* 
  3.  * Clipboard routines for the Amiga version of niftyterm.
  4.  * This code borrows heavily from the Clipboard code given
  5.  * in the AmigaMail tech notes, page XIV - 9.
  6.  * 
  7.  * Original code by Todd Williamson.
  8.  *
  9.  * $Author: ppessi $ $Revision: 2.0 $ $Date: 1993/11/15 03:35:51 $
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "nifty.h"
  15. #include "amiga.h"
  16.  
  17. #include <devices/clipboard.h>
  18.  
  19. static char clipopen = FALSE;
  20. static struct MsgPort *cport = NULL;
  21. static struct IOClipReq *creq = NULL;
  22.  
  23. /* Saves lots of assignment statements */
  24. #define SETIO(iob, com, buf, len) \
  25.     (iob)->io_Command = (com); \ 
  26.     (iob)->io_Data = (STRPTR)(buf); (iob)->io_Length = (len)
  27.  
  28. /* Used internally by the other clipboard routines.  Clipboard device is 
  29.  * automatically opened on the first read or write.  Must be closed
  30.  * explicitly.
  31.  */
  32. static int ClipOpen(void)
  33. {
  34.     clipopen = clipopen ||
  35.     ((cport = CreateMsgPort()) &&
  36.      (creq = (struct IOClipReq *)CreateIORequest(cport, sizeof(*creq))) &&
  37.      !OpenDevice("clipboard.device", 0, (struct IORequest *)creq, 0));
  38.     /* Free resources if necessary */
  39.     if (!clipopen) {
  40.     ClipClose();
  41.     puts("Could not open clipboard");
  42.     return -1;
  43.     }
  44.  
  45.     return 0;
  46. }
  47.  
  48. /* This will close the clipboard device if it is open.  Although the
  49.  * device is automatically opened on the first read or write, it must
  50.  * be closed explicitly.
  51.  */
  52. void ClipClose(void)
  53. {
  54.     if (clipopen) 
  55.         CloseDevice((struct IORequest *)creq);
  56.     clipopen = FALSE;
  57.  
  58.     if (creq)
  59.     DeleteIORequest((struct IORequest *)creq);
  60.     creq = NULL;
  61.  
  62.     if (cport)
  63.     DeleteMsgPort(cport);
  64.     cport = NULL;
  65. }
  66.  
  67. static void ClipLong(long *lword)
  68. {
  69.     SETIO(creq, CMD_WRITE, lword, 4);
  70.     DoIO((struct IORequest *)creq);
  71. }
  72.  
  73. /* Implement cutting of a string.  Cuts the string to the clipboard.
  74.  */
  75. int ClipCut(char *string)
  76. {
  77.     long length, slen;
  78.     int odd;
  79.  
  80.     if (ClipOpen() < 0) 
  81.     return -1;
  82.  
  83.     slen = strlen(string);
  84.     odd = (slen & 1);
  85.     length = slen + odd + 3 * sizeof(LONG);
  86.  
  87.     creq->io_ClipID = 0;
  88.     creq->io_Offset = 0;
  89.     ClipLong((LONG *)"FORM");
  90.     ClipLong(&length);
  91.     ClipLong((LONG *)"FTXT");
  92.     ClipLong((LONG *)"CHRS");
  93.     ClipLong(&slen);
  94.  
  95.     /* We make use of the last NUL at the end of the string */ 
  96.     SETIO(creq, CMD_WRITE, string, slen + odd);
  97.     DoIO((struct IORequest *)creq);
  98.     
  99.     creq->io_Command = CMD_UPDATE;
  100.     DoIO((struct IORequest *)creq);
  101.     return 0;
  102. }
  103.     
  104. /* Pass a buffer and a max size, and it gets filled with the current contents
  105.  * of the clipboard (if the contents is textual). Returns -1 on error, length
  106.  * if successful.  The buffer must be at least 8 characters.
  107.  */
  108. long ClipPaste(char *buf, long max)
  109. {
  110.     long length = 0, slen = 0, status = 0;
  111.     
  112.     if (ClipOpen() < 0) 
  113.     return 0;
  114.  
  115.     max--;
  116.     SETIO(creq, CMD_READ, buf, 4);
  117.     creq->io_Offset = 0;
  118.     creq->io_ClipID = 0;
  119.     status -= DoIO((struct IORequest *)creq);
  120.  
  121.     if(!strncmp(buf, "FORM", 4)) {
  122.         SETIO(creq, CMD_READ, &length, 4);
  123.     status -= DoIO((struct IORequest *)creq);
  124.     SETIO(creq, CMD_READ, buf, 8);
  125.     status -= DoIO((struct IORequest *)creq);
  126.     
  127.     if(!strncmp(buf, "FTXTCHRS", 8)) {
  128.         SETIO(creq, CMD_READ, &slen, 4);
  129.         status -= DoIO((struct IORequest *)creq);
  130.         
  131.         slen = MIN(slen, max);
  132.         SETIO(creq, CMD_READ, buf, slen);
  133.         status -= DoIO((struct IORequest *)creq);
  134.         
  135.         /* Force EOF on read */
  136.         SETIO(creq, CMD_READ, 0, 1);
  137.         creq->io_Offset += length;
  138.         do {
  139.           status -= DoIO((struct IORequest *)creq);
  140.         } while (creq->io_Actual);
  141.     }
  142.     }
  143.     if(status) return 0;
  144.     buf[slen] = '\0';
  145.     return slen;
  146. }
  147.